from bs4 import BeautifulSoup
import requests

# 微博热搜话题（要闻榜）
url = 'https://s.weibo.com/top/summary?cate=socialevent'

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Cache-Control': 'max-age=0',
    'Cookie': 'SUB=_2AkMRELAYf8NxqwFRmfwUyWzrZIx_wwHEieKnTEHDJRMxHRl-yT9yqlQAtRB6OpCe6C6iMTD6zow7KYnyNwCI5Cdb77Hq; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WhPlkP.mskRmjd1xfK9iVRh; _s_tentry=passport.weibo.com; Apache=5748995439659.872.1716272948645; SINAGLOBAL=5748995439659.872.1716272948645; ULV=1716272948662:1:1:1:5748995439659.872.1716272948645:; PC_TOKEN=32ec556417',
    'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

resp = requests.get(url, headers=headers, verify=False)

# print(resp.text)

# 解析数据
# 1、把网页源代码交给BeautifulSoup进行处理，生产bs对象
page = BeautifulSoup(resp.text, 'html.parser')

# 2、找到所有target="_blank"的对象
result_list = page.findAll(target="_blank")
# print(result_list)

# 3、遍历数组，拿到所有话题
for item in result_list:
    item_name = item.find_next('a').text
    print(item_name)